﻿using System;

namespace _1_ShapeClass
{
    abstract class shape
    {
        abstract public bool IsLegal();
        abstract public void show();
        abstract public string Name { get; }
        abstract public double Area { get; }
    }
    class Rectangle : shape
    {
        private double length, width;
        override public string Name
        {
            get { return "长方形";}
        }
        override public double Area
        {
            get { return length * width; }
        }
        public override bool IsLegal()
        {
            return (length > 0 && width > 0);
        }
        public Rectangle(double a, double b)
        {
            length = a; width = b;
            if (!IsLegal()) throw new ArgumentException("该形状不合法");
        }
        public Rectangle() { length = width = 0; }
        public override void show()
        {
            Console.WriteLine("该长方形的长为{0}，宽为{1}", length, width);
        }
    }
    class Square : shape
    {
        override public string Name
        {
            get { return "正方形"; }
        }
        override public double Area
        {
            get { return length * length; }
        }
            private double length;
        public override bool IsLegal()
        {
            return (length > 0);
        }
        public Square(double a) { length = a; if (!IsLegal()) throw new ArgumentException("该形状不合法"); }
        public Square() { length = 0; }
        public override void show()
        {
            Console.WriteLine("该正方形的边长为{0}", length);
        }
    }
    class Triangle : shape
    {
        private double a, b, c;
        override public string Name
        {
            get { return "三角形"; }
        }
        override public double Area
        {
            get { double p = (a + b + c) / 2.0; return Math.Sqrt(p * (p - a) * (p - b) * (p - c)); }
        }
            public override bool IsLegal()
        {
            return (a > 0 && b > 0 && c > 0 && a + b > c && b + c > a && a + c > b);
        }
        public Triangle(double d, double e, double f) { a = d; b = e; c = f; if (!IsLegal()) throw new ArgumentException("该形状不合法"); }
        public Triangle() { a = b = c = 0; }
        public override void show()
        {
            Console.WriteLine("该三角形的边长为{0}、{1}、{2}", a, b, c);
        }
    }
    class Program
    {
        static double read()
        {
            double res = 0; int flag = 0;
            while (flag == 0)
                try { res = double.Parse(Console.ReadLine()); flag = 1; }
                catch { Console.WriteLine("请重新输入"); }
            return res;
        }
        static void Main(string[] args)
        {
            shape thisShape;
            Console.Write("请输入数字选择图形:\n1.长方形\n2.正方形\n3.三角形\n");
            int type = int.Parse(Console.ReadLine());
            double a = 0, b = 0, c = 0;
            int flag = 1;
            switch (type)
            {
                case 1:
                    Console.WriteLine("请分两行输入长方形的两条边长：");
                    while (flag == 1)
                    {
                        try { a = read(); b = read(); thisShape = new Rectangle(a, b); flag = 0; }
                        catch { Console.WriteLine("请重新分两行输入长方形的两条边长："); }
                    }
                    thisShape = new Rectangle(a, b);
                    break;
                case 2:
                    Console.WriteLine("请行输入正方形的边长：");
                    while (flag == 1)
                    {
                        try { a = read(); thisShape = new Square(a); flag = 0; }
                        catch { Console.WriteLine("请重新输入正方形的边长："); }
                    }
                    thisShape = new Square(a);
                    break;
                case 3:
                    Console.WriteLine("请分三行输入三角形的两条边长：");
                    while (flag == 1)
                    {
                        try { a = read(); b = read(); c = read(); thisShape = new Triangle(a, b, c); flag = 0; }
                        catch { Console.WriteLine("请重新分三行输入三角形的两条边长："); }
                    }
                    thisShape = new Triangle(a, b, c);
                    break;
                default: thisShape = new Square(); break;
            }
            Console.WriteLine("该{0}的面积为{1}", thisShape.Name, thisShape.Area);
        }
    }
}